package com.common.base.tools.utils

import android.content.Context
import android.graphics.Canvas
import android.util.AttributeSet
import android.view.Gravity
import android.widget.ImageView
import android.widget.TextView
import androidx.constraintlayout.widget.ConstraintLayout
import com.common.base.R
import com.common.base.manager.StackManager
import com.common.base.tools.extend.click
import com.common.base.tools.extend.colorRes
import com.common.base.tools.extend.dimenRes
import com.common.base.tools.extend.px2dp

/**
 * 三疯
 * *************************
 * 半自定义View
 * 1、返回
 * 2、标题控制
 * 3、增加文字或者图标
 * *************************
 */
class CustomToolbar : ConstraintLayout {
    var attrs:AttributeSet?=null
    lateinit var backImageView:ImageView
    lateinit var rightImgeView:ImageView

    lateinit var titleTextView:TextView

    constructor(context: Context) : super(context) {
    }
    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) {
        this.attrs = attrs
    }
    init {
        initView()
    }


    private fun initView() {
        context?.let {
            //1、获取TypedArray
            val typedArray = context.obtainStyledAttributes(attrs, R.styleable.CustomToolbar)
            //返回按钮
            val backSrc = typedArray.getDrawable(R.styleable.CustomToolbar_backSrc)
            //标题
            val titleText = typedArray.getString(R.styleable.CustomToolbar_titleText)
            val titleTextColor =
                typedArray.getColor(R.styleable.CustomToolbar_titleTextColor, colorRes(R.color.colorRed))
            val titleTextSize = typedArray.getDimensionPixelOffset(R.styleable.CustomToolbar_titleTextSize,14)

            //右边图标
            val rightImageSrc = typedArray.getDrawable(R.styleable.CustomToolbar_rightImageSrc)

            backSrc?.let {
                val backImageViewLayout = LayoutParams(
                    LayoutParams.WRAP_CONTENT,
                    LayoutParams.MATCH_PARENT,
                )
                backImageViewLayout.leftToLeft = LayoutParams.PARENT_ID
                backImageViewLayout.topToTop = LayoutParams.PARENT_ID
                backImageViewLayout.bottomToBottom = LayoutParams.PARENT_ID
                backImageViewLayout.marginStart = dimenRes(R.dimen.dp_10)
                backImageView = ImageView(context)
                val dp10 = dimenRes(R.dimen.dp_20)
                backImageView.setPadding(dp10,dp10,dp10,dp10)
                backImageView.setImageDrawable(backSrc)
                backImageView.adjustViewBounds = true
                backImageView.layoutParams = backImageViewLayout
                addView(backImageView)
                backImageView.click {
                    StackManager.getActivityManager().currentActivity().finish()
                }

            }

            val titleTextViewLayout = LayoutParams(
                LayoutParams.MATCH_PARENT,
                LayoutParams.MATCH_PARENT,
            )
            titleTextViewLayout.leftToLeft = LayoutParams.PARENT_ID
            titleTextViewLayout.topToTop = LayoutParams.PARENT_ID
            titleTextViewLayout.bottomToBottom = LayoutParams.PARENT_ID
            titleTextViewLayout.rightToRight = LayoutParams.PARENT_ID
            titleTextView = TextView(context)
            titleTextView.text = titleText ?: ""
            titleTextView.textSize = px2dp(titleTextSize).toFloat()
            titleTextView.setTextColor(titleTextColor)
            titleTextView.gravity = Gravity.CENTER
            titleTextView.layoutParams = titleTextViewLayout
            addView(titleTextView)

            rightImageSrc?.let {
                val rightImgeViewLayout = LayoutParams(
                    LayoutParams.WRAP_CONTENT,
                    LayoutParams.MATCH_PARENT,
                )
                rightImgeViewLayout.topToTop = LayoutParams.PARENT_ID
                rightImgeViewLayout.bottomToBottom = LayoutParams.PARENT_ID
                rightImgeViewLayout.rightToRight = LayoutParams.PARENT_ID
                rightImgeViewLayout.marginEnd = dimenRes(R.dimen.dp_10)
                rightImgeView = ImageView(context)
                rightImgeView.setImageDrawable(rightImageSrc)
                rightImgeView.adjustViewBounds = true
                rightImgeView.layoutParams = rightImgeViewLayout
                addView(rightImgeView)
            }
        }
    }

    fun backImgClick(callback:()->Unit){
        if(this::backImageView.isInitialized){
            backImageView.click {
                callback.invoke()
            }
        }
    }

    fun rightImgClick(callback:()->Unit){
        if(this::rightImgeView.isInitialized){
            rightImgeView.click {
                callback.invoke()
            }
        }
    }

}